<?php


class Lion_Locale extends Zend_Locale
{

    static $_suplangs = array();

    /**
     * Generates a locale object
     * If no locale is given a automatic search is done
     * Then the most probable locale will be automatically set
     * Search order is
     *  1. Given Locale
     *  2. HTTP Client
     *  3. Server Environment
     *  4. Framework Standard
     *
     * @param  string|Zend_Locale $locale (Optional) Locale for parsing input
     * @throws Zend_Locale_Exception When autodetection has been failed
     */
    public function __construct ($locale = null)
    {

        $locale = self::_prepareLocale($locale, false);
        $this->setLocale((string) $locale);
    }

    public static function setLangs (Zend_Config $langs)
    {

        foreach ($langs as $key => $val) {
            self::$_suplangs[$key] = $val;
        }
    }

    /**
     * Internal function, returns a single locale on detection
     *
     * @param  string|Zend_Locale $locale (Optional) Locale to work on
     * @param  boolean            $strict (Optional) Strict preparation
     * @throws Zend_Locale_Exception When no locale is set which is only possible when the class was wrong extended
     * @return string
     */
    private static function _prepareLocale ($locale, $strict = false)
    {

        if ($locale instanceof Zend_Locale) {
            $locale = $locale->toString();
        }

        if (is_array($locale)) {
            return '';
        }

        if (empty(parent::$_auto) === true) {
            parent::$_browser = parent::getBrowser();
            parent::$_environment = parent::getEnvironment();
            //parent::$_breakChain  = true;
            parent::$_auto = parent::getBrowser() + parent::getEnvironment() + parent::getDefault();

        }

        if (! $strict) {
                   
            if ($locale === 'browser') {
                $locale = parent::$_browser;
            }

            if ($locale === 'environment') {
                $locale = parent::$_environment;
            }

            if ($locale === 'default') {
                $locale = parent::$_default;
            }

            if (($locale === 'auto') or ($locale === null)) {
                $locale = parent::$_auto;
            }
			            
			$sess = new Zend_Session_Namespace('locale');
			if(isset($sess->lang_key)){
			  $locale = $sess->lang_key;
			}
			
            if (is_array($locale) === true) {
                foreach($locale as $key=>$value){
                	if(array_key_exists($key,self::$_suplangs)){
                		$locale = $key;
                		break;
                	}
                }
            }
            $sess->lang_key = $locale;
        }

        // This can only happen when someone extends Zend_Locale and erases the default
        if (! is_string($locale)) {
            require_once 'Zend/Locale/Exception.php';
            throw new Zend_Locale_Exception('Autodetection of Locale has been failed!');
        }

        if (strpos($locale, '-') !== false) {
            $locale = strtr($locale, '-', '_');
        }

        return (string) $locale;
    }
}
